package com.guigu.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.mapper.MerchantInfoMapper;
import com.guigu.youxuan.pojo.MerchantInfo;
import com.guigu.youxuan.pojo.dto.SysMerchantDto;
import com.guigu.youxuan.service.IMerchantInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class MerchantInfoServiceImpl extends ServiceImpl<MerchantInfoMapper, MerchantInfo> implements IMerchantInfoService {

    @Autowired
    private MerchantInfoMapper merchantInfoMapper;

    @Override
    public PageInfo<MerchantInfo> getMerchantList(int pageNo, int pageSize, MerchantInfo merchantInfo) {
        QueryWrapper<MerchantInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);

        // 添加额外的查询条件
        if (merchantInfo.getName() != null) {
            wrapper.eq("name", merchantInfo.getName());
        }
        if (merchantInfo.getShopName() != null) {
            wrapper.eq("shop_name", merchantInfo.getShopName());
        }

        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        // 查询语句
        List<MerchantInfo> merchantInfos = merchantInfoMapper.queryAll(merchantInfo);
        // 封装分页结果
        return new PageInfo<>(merchantInfos);
    }

    @Override
    public PageUtils<MerchantInfo> merchanrInfoFuHePage(MerchantInfo merchantInfo, int pageNo, int pageSize) {
        QueryWrapper<MerchantInfo> wrapper=new QueryWrapper<MerchantInfo>();
        //额外加2个条件
        //审核状态: 待审核
        wrapper.eq("check_Tag","S001-1");

        PageHelper.startPage(pageNo,pageSize);
        List<MerchantInfo> merchantInfos=this.list(wrapper);
        PageInfo<MerchantInfo> pageInfo=new PageInfo<MerchantInfo>(merchantInfos);

        //把PageHelper PageUtils
        PageUtils<MerchantInfo> pageUtils=new PageUtils<MerchantInfo>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    @Override
    public void delByMerchantId(MerchantInfo merchantInfo) {
        if (merchantInfo != null && merchantInfo.getMerchantId() != null) {
            // 构建更新的值和条件
            UpdateWrapper<MerchantInfo> updateWrapper = new UpdateWrapper<>();

            // 设置要更新的列和新值
            updateWrapper.set("is_deleted", 1)
                    .eq("merchant_id", merchantInfo.getMerchantId())
                    .eq("is_deleted", 0);
            // 确保仅更新未删除的记录

            // 执行更新操作
            this.update(merchantInfo, updateWrapper);
        }
    }

    @Override
    public List<MerchantInfo> selAddress() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("check_tag","S001-3");
        wrapper.eq("is_deleted",0);
        List selAddressList=this.list(wrapper);
        return selAddressList;
    }

    @Override
    public boolean updateByDtoId(SysMerchantDto dto) {
        try {
            merchantInfoMapper.updateMerchantInfo(dto);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public List<MerchantInfo> SelById(int sysUsersId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("sys_users_id",sysUsersId);
        List sysUsersIds=this.list(queryWrapper);
        return sysUsersIds;
    }
}
